package com.yinhai.springmvc.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.yinhai.springmvc.po.Emp;

/**
 * 
 * @author 志宇
 *
 */
public class EmpsController2 implements HttpRequestHandler{

	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Emp emp1 = new Emp();
		emp1.setSal(new BigDecimal(5666));
		emp1.setEmpno(222);
		emp1.setEname("何何");
		Emp emp2 = new Emp();
		emp2.setSal(new BigDecimal(6400));
		emp2.setEmpno(333);
		emp2.setEname("西西");
		
		Emp emp3 = new Emp();
		emp3.setSal(new BigDecimal(6999));
		emp3.setEmpno(444);
		emp3.setEname("哈铪");
		
		List empList = new ArrayList();
		empList.add(emp1);
		empList.add(emp2);
		empList.add(emp3);
		//设置模型数据
		request.setAttribute("empList", empList);
		//设置转发的视图
		request.getRequestDispatcher("/WEB-INF/jsp/emps/empList.jsp").forward(request, response);
		
		//使用此方法可以通过修改response设置响应的格式，比如响应json数据
		/*response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write("json");*/
		
	}
	
	

	/*@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		Emp emp1 = new Emp();
		emp1.setSal(new BigDecimal(5000));
		emp1.setEmpno(222);
		emp1.setEname("何何");
		Emp emp2 = new Emp();
		emp2.setSal(new BigDecimal(5400));
		emp2.setEmpno(333);
		emp2.setEname("西西");
		
		Emp emp3 = new Emp();
		emp3.setSal(new BigDecimal(5900));
		emp3.setEmpno(444);
		emp3.setEname("哈铪");
		
		List empList = new ArrayList();
		empList.add(emp1);
		empList.add(emp2);
		empList.add(emp3);
		//返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		//相当于request.setAttribute,在jsp页面取数据
		modelAndView.addObject("empList", empList);
		
		//指定视图
		modelAndView.setViewName("/WEB-INF/jsp/emps/empList.jsp");
		
		
		return modelAndView;
	}*/
	
}
